package com.gxa.jjty.cjr.controller;


import com.gxa.jjty.cjr.service.CjrUserService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.CjrUser;
import com.gxa.jjty.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author NITRO
 */
@Api(tags = "用户信息api")
@RestController
@RequestMapping("/sso")
public class CjrUserController {


    @Autowired
    private CjrUserService cjrUserService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Map login(@RequestParam("phone") String phone, @RequestParam("code") String code) throws BusinessException {

        if (StringUtil.isEmpty(phone)){
            throw new BusinessException(ExceptionCode.BAD_PHONE);
        }
//        if (StringUtil.isPhoneNumber(phone)) {
//            throw new BusinessException(ExceptionCode.WRONG_PHONE_NUMBER);
//        }
        if (StringUtil.isEmpty(code)){
            throw new BusinessException(ExceptionCode.NOT_NULL);
        }
        CjrUser cjrUser = cjrUserService.selectUser(phone);

        JsonResult jsonResult = new JsonResult();
        Map<String, String> login = cjrUserService.login(phone,code);
        String token = login.get("token");
        cjrUser.setToken(token);
        jsonResult.setData(cjrUser);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public Map logout(@RequestParam("phone") String phone){
        cjrUserService.logout(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("查询用户档案信息")
    public Map<String,Object> selectById(Long id) throws BusinessException {
        if (id==null){
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        return cjrUserService.selectById(id);
    }

    @PostMapping("/feedback")
    @ApiOperation("意见反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
            @ApiImplicitParam(name = "content",value = "反馈内容")
    })
    public Map<String, Object> feedback(Long id,String content) throws BusinessException {
        if (id==null){
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        if (content==null){
            throw new BusinessException(ExceptionCode.CONTENT_NOTNULL);
        }
        return cjrUserService.feedback(id, content);
    }

    @PostMapping("/messageInform")
    @ApiOperation("消息通知")
    @ApiImplicitParam(name = "id",value = "用户id")
    public Map<String,Object> messageInform(Long id) throws BusinessException {
        if (id==null){
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        return cjrUserService.messageInform(id);
    }
}
